<?php

/**
 * RP System
 *
 * @version 1.0
 * @copyright 2004 Ron Planken
 */

class profile {

	
	function get($player_id) {

		global $dbh;

			$query ="SELECT player_id
						   ,name
						   ,level
						   ,class
						   ,race
						   ,gender
						   ,talent1
						   ,talent2
						   ,talent3
						   ,profession_1_name
						   ,profession_1_rank
						   ,profession_2_name
						   ,profession_2_rank
						   ,health
						   ,`second`
						   ,second_type
						   ,strength
						   ,agility
						   ,stamina
						   ,intellect
						   ,spirit
						   ,armor
						   ,power
						   ,hitrating
						   ,critchance
						   ,expertise
						   ,rpower
						   ,rhitrating
						   ,rcritchance
						   ,spellArcanePower
						   ,spellFirePower
						   ,spellFrostPower
						   ,spellHolyPower
						   ,spellNaturePower
						   ,spellShadowPower
						   ,healing
						   ,shitrating
						   ,spellArcaneCrit
						   ,spellFireCrit
						   ,spellFrostCrit
						   ,spellHolyCrit
						   ,spellNatureCrit
						   ,spellShadowCrit
						   ,spellPenetration
						   ,manaRegen
						   ,defense
						   ,dodge
						   ,parry
						   ,block
						   ,resilience
						   ,last_updated
					   FROM profile
					  WHERE player_id = $player_id

							";

		$player = selectMultiQuery($query, $dbh);

		return $player[0];
	}
	
	function getArmory($player_name, $player_id) {
		
		$cURL = curl_init();
		
		$player_name = utf8_encode($player_name);
		
		// Set the URL to execute
		curl_setopt($cURL, CURLOPT_URL, "http://eu.wowarmory.com/character-sheet.xml?r=Tarren+Mill&n=$player_name");
		
		// Set options
		
		curl_setopt($cURL, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11');
		curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
		
		// Execute, saving results in a variable
		$strPage = curl_exec($cURL);
		
		// Close CURL resource
		curl_close($cURL);

		$xml = simplexml_load_string($strPage);
		
		//echo $xml->characterInfo->character['class'];
    
		$parse_profile = $this->parseData($xml);
		
		$parse_profile['player_id'] = $player_id;
		
		$this->save($parse_profile);
		
	}

	function parseData($xml_data)
	{
		
		$parse_profile['name'] = utf8_decode(secure_string($xml_data->characterInfo->character['name']));
		
		$parse_profile['level'] = secure_int($xml_data->characterInfo->character['level']);
		
		$parse_profile['class'] = secure_string($xml_data->characterInfo->character['class']);
		
		$parse_profile['race'] = secure_string($xml_data->characterInfo->character['race']);
		
		$parse_profile['gender'] = secure_string($xml_data->characterInfo->character['gender']);
		
		$parse_profile['talent1'] = secure_int($xml_data->characterInfo->characterTab->talentSpec['treeOne']);
		
		$parse_profile['talent2'] = secure_int($xml_data->characterInfo->characterTab->talentSpec['treeTwo']);
		
		$parse_profile['talent3'] = secure_int($xml_data->characterInfo->characterTab->talentSpec['treeThree']);
		
		$parse_profile['profession_1_name'] = secure_string($xml_data->characterInfo->characterTab->professions->skill[0]['name']);
		
		$parse_profile['profession_1_rank'] = secure_int($xml_data->characterInfo->characterTab->professions->skill[0]['value']);
		
		$parse_profile['profession_2_name'] = secure_string($xml_data->characterInfo->characterTab->professions->skill[1]['name']);
		
		$parse_profile['profession_2_rank'] = secure_int($xml_data->characterInfo->characterTab->professions->skill[1]['value']);
				
		$parse_profile['health'] = secure_int($xml_data->characterInfo->characterTab->characterBars->health['effective']);
		
		$parse_profile['second'] = secure_int($xml_data->characterInfo->characterTab->characterBars->secondBar['effective']);
		
		$parse_profile['second_type'] = secure_string($xml_data->characterInfo->characterTab->characterBars->secondBar['type']);
		
		$parse_profile['strength'] = secure_int($xml_data->characterInfo->characterTab->baseStats->strength['effective']);
		
		$parse_profile['agility'] = secure_int($xml_data->characterInfo->characterTab->baseStats->agility['effective']);
		
		$parse_profile['stamina'] = secure_int($xml_data->characterInfo->characterTab->baseStats->stamina['effective']);
		
		$parse_profile['intellect'] = secure_int($xml_data->characterInfo->characterTab->baseStats->intellect['effective']);
		
		$parse_profile['spirit'] = secure_int($xml_data->characterInfo->characterTab->baseStats->spirit['effective']);
		
		$parse_profile['armor'] = secure_int($xml_data->characterInfo->characterTab->baseStats->armor['effective']);
		
		$parse_profile['power'] = secure_int($xml_data->characterInfo->characterTab->melee->power['effective']);
		
		$parse_profile['hitrating'] = secure_int($xml_data->characterInfo->characterTab->melee->hitRating['value']);
		
		$parse_profile['critchance'] = secure_int($xml_data->characterInfo->characterTab->melee->critChance['percent']);

		$parse_profile['expertise'] = secure_int($xml_data->characterInfo->characterTab->melee->expertise['value']);			
		
		$parse_profile['rpower'] = secure_int($xml_data->characterInfo->characterTab->ranged->power['effective']);			
		
		$parse_profile['rhitrating'] = secure_int($xml_data->characterInfo->characterTab->ranged->hitRating['value']);			
		
		$parse_profile['rcritchance'] = secure_int($xml_data->characterInfo->characterTab->ranged->critChance['percent']);			
		
		$parse_profile['spellArcanePower'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusDamage->arcane['value']);			
		
		$parse_profile['spellFirePower'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusDamage->fire['value']);			
		
		$parse_profile['spellFrostPower'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusDamage->frost['value']);			
		
		$parse_profile['spellHolyPower'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusDamage->holy['value']);			
		
		$parse_profile['spellNaturePower'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusDamage->nature['value']);			
		
		$parse_profile['spellShadowPower'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusDamage->shadow['value']);			
		
		$parse_profile['healing'] = secure_int($xml_data->characterInfo->characterTab->spell->bonusHealing['value']);			
		
		$parse_profile['shitrating'] = secure_int($xml_data->characterInfo->characterTab->spell->hitRating['increasedHitPercent']);			
		
		$parse_profile['spellArcaneCrit'] = secure_int($xml_data->characterInfo->characterTab->spell->critChance->arcane['percent']);
		
		$parse_profile['spellFireCrit'] = secure_int($xml_data->characterInfo->characterTab->spell->critChance->fire['percent']);
		
		$parse_profile['spellFrostCrit'] = secure_int($xml_data->characterInfo->characterTab->spell->critChance->frost['percent']);
		
		$parse_profile['spellHolyCrit'] = secure_int($xml_data->characterInfo->characterTab->spell->critChance->holy['percent']);
		
		$parse_profile['spellNatureCrit'] = secure_int($xml_data->characterInfo->characterTab->spell->critChance->nature['percent']);
		
		$parse_profile['spellShadowCrit'] = secure_int($xml_data->characterInfo->characterTab->spell->critChance->shadow['percent']);
		
		$parse_profile['spellPenetration'] = secure_int($xml_data->characterInfo->characterTab->spell->penetration['value']);
		
		$parse_profile['manaRegen'] = secure_int($xml_data->characterInfo->characterTab->spell->manaRegen['notCasting']);
		
		$parse_profile['defense'] = secure_int($xml_data->characterInfo->characterTab->defenses->defense['value'] + $xml_data->characterInfo->characterTab->defenses->defense['plusDefense']);
		
		$parse_profile['dodge'] = secure_int($xml_data->characterInfo->characterTab->defenses->dodge['percent']);
		
		$parse_profile['parry'] = secure_int($xml_data->characterInfo->characterTab->defenses->parry['percent']);
		
		$parse_profile['block'] = secure_int($xml_data->characterInfo->characterTab->defenses->block['percent']);
		
		$parse_profile['resilience'] = secure_int($xml_data->characterInfo->characterTab->defenses->resilience['value']);
		
		$parse_profile['last_updated'] = "Now()";
		
		return $parse_profile;		
	}
	
	function checkForData($player_id)
	{
		global $dbh;
		
		
		$query ="SELECT Count(*) as player
				   FROM profile
				  WHERE player_id = $player_id;
						";

		$count = selectQuery($query, $dbh);
		
		return $count;
	}
	
	function checkForUpdate($player_id)
	{
		global $dbh;
		
		
		$query ="SELECT last_updated as last_updated
				   FROM profile
				  WHERE player_id = $player_id;
						";

		$last_updated = selectQuery($query, $dbh);
		
		return $last_updated;
	}
	
	function save($profile)
	{
		global $dbh;
		
		$count = $this->checkForData($profile['player_id']);
	
		if ($count == 0) {
			$query ="INSERT INTO profile
						   (player_id
						   ,name
						   ,level
						   ,class
						   ,race
						   ,gender
						   ,talent1
						   ,talent2
						   ,talent3
						   ,profession_1_name
						   ,profession_1_rank
						   ,profession_2_name
						   ,profession_2_rank
						   ,health
						   ,`second`
						   ,second_type
						   ,strength
						   ,agility
						   ,stamina
						   ,intellect
						   ,spirit
						   ,armor
						   ,power
						   ,hitrating
						   ,critchance
						   ,expertise
						   ,rpower
						   ,rhitrating
						   ,rcritchance
						   ,spellArcanePower
						   ,spellFirePower
						   ,spellFrostPower
						   ,spellHolyPower
						   ,spellNaturePower
						   ,spellShadowPower
						   ,healing
						   ,shitrating
						   ,spellArcaneCrit
						   ,spellFireCrit
						   ,spellFrostCrit
						   ,spellHolyCrit
						   ,spellNatureCrit
						   ,spellShadowCrit
						   ,spellPenetration
						   ,manaRegen
						   ,defense
						   ,dodge
						   ,parry
						   ,block
						   ,resilience
						   ,last_updated)
					VALUES ({$profile['player_id']}
						   ,{$profile['name']}
						   ,{$profile['level']}
						   ,{$profile['class']}
						   ,{$profile['race']}
						   ,{$profile['gender']}
						   ,{$profile['talent1']}
						   ,{$profile['talent2']}
						   ,{$profile['talent3']}
						   ,{$profile['profession_1_name']}
						   ,{$profile['profession_1_rank']}
						   ,{$profile['profession_2_name']}
						   ,{$profile['profession_2_rank']}
						   ,{$profile['health']}
						   ,{$profile['second']}
						   ,{$profile['second_type']}
						   ,{$profile['strength']}
						   ,{$profile['agility']}
						   ,{$profile['stamina']}
						   ,{$profile['intellect']}
						   ,{$profile['spirit']}
						   ,{$profile['armor']}
						   ,{$profile['power']}
						   ,{$profile['hitrating']}
						   ,{$profile['critchance']}
						   ,{$profile['expertise']}
						   ,{$profile['rpower']}
						   ,{$profile['rhitrating']}
						   ,{$profile['rcritchance']}
						   ,{$profile['spellArcanePower']}
						   ,{$profile['spellFirePower']}
						   ,{$profile['spellFrostPower']}
						   ,{$profile['spellHolyPower']}
						   ,{$profile['spellNaturePower']}
						   ,{$profile['spellShadowPower']}
						   ,{$profile['healing']}
						   ,{$profile['shitrating']}
						   ,{$profile['spellArcaneCrit']}
						   ,{$profile['spellFireCrit']}
						   ,{$profile['spellFrostCrit']}
						   ,{$profile['spellHolyCrit']}
						   ,{$profile['spellNatureCrit']}
						   ,{$profile['spellShadowCrit']}
						   ,{$profile['spellPenetration']}
						   ,{$profile['manaRegen']}
						   ,{$profile['defense']}
						   ,{$profile['dodge']}
						   ,{$profile['parry']}
						   ,{$profile['block']}
						   ,{$profile['resilience']}
						   ,{$profile['last_updated']})
							";
			
			$status = InsertQuery($query, $dbh);

			$status = mysql_insert_id();

		} else {
			$query ="UPDATE profile
						SET name = {$profile['name']}
						   ,level = {$profile['level']}
						   ,class = {$profile['class']}
						   ,race = {$profile['race']}
						   ,gender = {$profile['gender']}
						   ,talent1 = {$profile['talent1']}
						   ,talent2 = {$profile['talent2']}
						   ,talent3 = {$profile['talent3']}
						   ,profession_1_name = {$profile['profession_1_name']}
						   ,profession_1_rank = {$profile['profession_1_rank']}
						   ,profession_2_name = {$profile['profession_2_name']}
						   ,profession_2_rank = {$profile['profession_2_rank']}
						   ,health = {$profile['health']}
						   ,`second` = {$profile['second']}
						   ,second_type = {$profile['second_type']}
						   ,strength  ={$profile['strength']}
						   ,agility  ={$profile['agility']}
						   ,stamina  ={$profile['stamina']}
						   ,intellect  ={$profile['intellect']}
						   ,spirit  ={$profile['spirit']}
						   ,armor  ={$profile['armor']}
						   ,power  ={$profile['power']}
						   ,hitrating  ={$profile['hitrating']}
						   ,critchance  ={$profile['critchance']}
						   ,expertise  ={$profile['expertise']}
						   ,rpower  ={$profile['rpower']}
						   ,rhitrating  ={$profile['rhitrating']}
						   ,rcritchance  ={$profile['rcritchance']}
						   ,spellArcanePower  ={$profile['spellArcanePower']}
						   ,spellFirePower  ={$profile['spellFirePower']}
						   ,spellFrostPower  ={$profile['spellFrostPower']}
						   ,spellHolyPower  ={$profile['spellHolyPower']}
						   ,spellNaturePower  ={$profile['spellNaturePower']}
						   ,spellShadowPower  ={$profile['spellShadowPower']}
						   ,healing  ={$profile['healing']}
						   ,shitrating  ={$profile['shitrating']}
						   ,spellArcaneCrit  ={$profile['spellArcaneCrit']}
						   ,spellFireCrit  ={$profile['spellFireCrit']}
						   ,spellFrostCrit  ={$profile['spellFrostCrit']}
						   ,spellHolyCrit  ={$profile['spellHolyCrit']}
						   ,spellNatureCrit  ={$profile['spellNatureCrit']}
						   ,spellShadowCrit  ={$profile['spellShadowCrit']}
						   ,spellPenetration  ={$profile['spellPenetration']}
						   ,manaRegen  ={$profile['manaRegen']}
						   ,defense  ={$profile['defense']}
						   ,dodge  ={$profile['dodge']}
						   ,parry  ={$profile['parry']}
						   ,block  ={$profile['block']}
						   ,resilience  ={$profile['resilience']}
						   ,last_updated = {$profile['last_updated']}
					  WHERE player_id = {$profile['player_id']}
							";

			$status = UpdateQuery($query, $dbh);
		}

		return $status;

		
		
	}
	
}
?>